ALTER PROCEDURE [dbo].[tWfNOSubBackState] (@wfId int,@curSendId int)
AS
BEGIN
	declare @result varchar(20),@total int=0,@reply_num int=0,@back_num int=0,@send_num int=0,@sendId int=0,@deptNum int=0,@backTime datetime,@sendState int,@deptId varchar(20),@pid int;
	select @deptId=SDeptID,@pid=pid from tWfSendBack where IsDel=0 and PkId=@curSendId
	if @pid > 0
	begin
	   select @total=count(1) from tWfSendBack where IsDel=0 and wfId=@wfId AND pid=@pid
	   if @total = 1
	   begin
	     select  @sendState=SendState,@backTime=BackTime FROM tWfSendBack where IsDel=0 and SendState not in(30) and wfId=@wfId AND pid=@pid
		 if @sendState = 40 set @result = '全退'
		 else if @backTime is null set @result = '未回复' else set @result = '已回复'
	   end
	   else if @total > 1
	   begin
	     select @send_num=count(1) FROM tWfSendBack where IsDel=0 and SendState not in(30,40) and wfId=@wfId AND sdeptId=@deptId and pkId>=@sendId
		 select @back_num=count(1) FROM tWfSendBack where IsDel=0 and SendState=40 and wfId=@wfId AND sdeptId=@deptId and pkId>=@sendId
		 select @reply_num=count(1) FROM tWfSendBack where IsDel=0 and SendState not in(30,40) and wfId=@wfId AND sdeptId=@deptId and BackTime is not null and pkId>=@sendId
		 if @send_num>0
		 begin
		   if @reply_num=0 set @result = '未回复'
		   else if @reply_num>0 and @reply_num < @send_num set @result = '未全回'
		   else if @reply_num=@send_num set @result = '已回复'
		 end
		 if @back_num>0 and @back_num=@total
		   set @result = '全退'
	   end
	   --更新父级派单状态
	   update tWfSendBack set subBackState=@result where PkId=@pid
	end
	else
	begin
	    --初次 再次 三次派单，只取最新的那块
		select top 1 @sendId=PkId from tWfSendBack where IsDel=0 and WfId=@wfId and SeqCode=(select max(seqCode) from tWfSendBack where IsDel=0 and WfId=@wfId) order by PkId
		select @total=count(1),@deptNum=count(distinct SBDeptId) FROM tWfSendBack where IsDel=0 and SendState not in(30) and wfId=@wfId AND sdeptId=@deptId and pkId>=@sendId
		--重复派给一个部门
		if @deptNum = 1
		begin
			--只取最新的
			select top 1 @sendState=SendState,@backTime=BackTime FROM tWfSendBack where IsDel=0 and SendState not in(30) and wfId=@wfId AND sdeptId=@deptId and pkId>=@sendId order by PkId desc
			if @sendState = 40 set @result = '全退'
			else if @backTime is null set @result = '未回复' else set @result = '已回复'
		end
		--派给多个部门
		else if @deptNum > 1
		begin
			select @send_num=count(1) FROM tWfSendBack where IsDel=0 and SendState not in(30,40) and wfId=@wfId AND sdeptId=@deptId and pkId>=@sendId
			select @back_num=count(1) FROM tWfSendBack where IsDel=0 and SendState=40 and wfId=@wfId AND sdeptId=@deptId and pkId>=@sendId
			select @reply_num=count(1) FROM tWfSendBack where IsDel=0 and SendState not in(30,40) and wfId=@wfId AND sdeptId=@deptId and BackTime is not null and pkId>=@sendId
			if @send_num>0
			begin
			  if @reply_num=0 set @result = '未回复'
			  else if @reply_num>0 and @reply_num < @send_num set @result = '未全回'
			  else if @reply_num=@send_num set @result = '已回复'
			end
			if @back_num>0 and @back_num=@total
			  set @result = '全退'
		end
		--更新父级派单状态
		select top 1 @sendId=pkId from tWfSendBack where WfId=@wfId and SBDeptID=@deptId  order by PkId desc
		update tWfSendBack set subBackState=@result where PkId=@sendId
	end
END